<template>
	<view>
		<navigator :url="`/pages/brother/order/views/views?id=${res.id}`" class="order" v-for="(res, index) in orderList" :key="res.id">
			<view class="top u-border-bottom u-p-b-20">
				<view class="left">
					<view><text class="u-tips-color">{{ res.number }}</text></view>
					<view><text class="u-font-24 u-tips-color"><text class="bold-txt">{{$u.timeFrom(new Date(res.createddatetime.replace(/-/g,'/')),false)}}下单</text></text></view>
				</view>
				<view class="right">
					<text v-if="res.managestate == '已回收' || res.managestate == '已入库'" class="color-success">{{ res.managestate }}</text>
					<text v-else-if="res.managestate == '已取消'" class="color-danger">{{ res.managestate }}</text>
					<text v-else>{{ res.managestate }}</text>
				</view>
			</view>
			<view class="msg-box u-p-t-10">
				<view class="u-flex">
					<view class="u-flex-1 u-p-r-20">
						<view class="usertype">
							<text class="u-font-bold">[{{res.address.cityname}}]</text>
							<text>{{res.address.address}}</text>
						</view>
						<view class="address">
							<!-- <u-tag v-if="res.enterprise" text="客户组" mode="dark" size="mini"></u-tag>
							<u-tag v-else text="用户" mode="dark" size="mini"></u-tag> -->
							<text>{{res.address.name}}</text>
							<text class="u-p-l-10">{{hidePhoneNumber(res.address.mobile)}}</text>
						</view>
						<!-- <view class="address">
							<u-icon name="phone-fill"></u-icon>
							<text>{{res.address.mobile}}</text>
						</view> -->
						<view class="u-p-t-10">
							<u-tag v-for="(c,k) in res.estimated" :key="k" :text="c.names" mode="plain" size="mini" class="u-m-r-10"></u-tag>
						</view>
					</view>
					<view class="distance" v-if="res.distance && (res.managestate == '待接单' || res.managestate == '已接单' || res.managestate == '待上门')">
						<view class="u-tips-color"><text>距离直线</text></view>
						<view><text class="u-font-40">{{res.distance}}</text><text class="u-font-24">KM</text>
						</view>
					</view>
				</view>
			</view>
			<view class="bottom u-border-top u-p-t-20">
				<view class="u-flex-1">
					<view class="u-flex">
						<!-- 待接单 -->
						<block v-if="res.managestate == '待接单'">
							<view class="u-flex u-flex-1">
								<!-- <view class="btn" @click.stop="clickRefuse(res.id)">拒单</view> -->
								<view class="btn evaluate" @click.stop="receiving(res.id)">点击接单</view>
							</view>
						</block>
						<!-- 已接单 -->
						<block v-if="res.managestate == '已接单' || res.managestate == '待上门' || res.managestate == '待付款'">
							<view class="more u-flex-1">
								<view class="cell" @click.stop="goMap(res.address)">
									<u-icon name="map-fill" size="32" class="tips-txt"></u-icon>
									<view>
										<text class="u-font-20">导航</text>
									</view>
								</view>
								<view class="divider"></view>
								<view class="cell" @click.stop="call(res.address.mobile)">
									<u-icon name="phone-fill" size="32" class="tips-txt"></u-icon>
									<view>
										<text class="u-font-20">联系</text>
									</view>
								</view>
							</view>
							<view class="u-flex u-flex-1">
								<navigator v-if="res.managestate == '待付款'" class="btn details" @click.stop :url="`/pages/brother/order/settlement/settlement?orderId=${res.id}`">立即结算</navigator>
								<navigator v-else class="btn details">查看详情</navigator>
							</view>
						</block>
						<block v-if="res.managestate == '已取消'">
							<view class="u-flex u-flex-1">
								<text class="tips-txt">订单已取消</text>
							</view>
						</block>
						<block v-if="res.managestate == '已回收' || res.managestate == '已入库'">
							<view class="u-flex u-flex-1">
								<text class="tips-txt">订单已于{{res.completiontime}}完成</text>
							</view>
						</block>
						<!-- <block v-else>
							<view class="u-flex u-flex-1">
								<navigator class="btn details" :url="`../order/views/views?id=${res.id}`">查看详情</navigator>
							</view>
						</block> -->
					</view>
				</view>
			</view>
		</navigator>
		
		<!-- 拒绝订单 -->
		<u-popup v-model="showCancel" mode="center" border-radius="14" z-index="999" closeable>
			<view class="u-p-40" style="width: 600rpx;">
				<view class="u-p-t-30 u-p-b-15">请填写拒绝订单原因：</view>
				<view class="u-p-b-30">
					<u-input v-model="cancelForm.reason" type="textarea" :border="true" />
				</view>
				<u-button type="error" class="navbtn" @click="refuse">确定</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	export default {
		props:{
			orderList: {
				type: Array,
				default: () => {
					return []
				}
			}
		},
		data() {
			return {
				// 拒单
				showCancel: false,
				cancelForm: {
					id: 0,
					reason: ""
				},
			}
		},
		computed: {
			...mapState({
				userdetail: state => state.userdetail
			}),
			...mapState('brother',{
				online: state => state.online,
				brotherTrip: state => state.brotherTrip
			})
		},
		methods:{
			// 接单
			receiving(id) {
				// if (!this.online) {
				// 	uni.showToast({
				// 		title: '请先上线',
				// 		icon: 'none'
				// 	})
				// 	return
				// }
				uni.showModal({
					title: '提示',
					content: '您确定要接受订单吗?',
					success: (res) => {
						if (res.confirm) {
							// this.current = 1
							this.$u.get('/User/Order/Receiving?id=' + id).then((res) => {
								uni.showToast({
									icon: 'none',
									title: res.msg
								})
								this.current = 1
								// this.getOrderList('已接单',this.current,true)
								uni.navigateTo({
									url: '/pages/brother/order/views/views?id=' + id
								})
							})
						}
					}
				})
			},
			// 点击拒单
			clickRefuse(id) {
				this.showCancel = true
				this.cancelForm.reason = ''
				this.cancelForm.id = id
			},
			// 拒单
			refuse() {
				uni.showModal({
					title: '提示',
					content: '您确定要拒绝订单吗?',
					success: (res) => {
						if (res.confirm) {
							this.$u.post('/User/Order/Refuse', this.cancelForm).then((res) => {
								uni.showToast({
									icon: 'none',
									title: res.msg
								})
								this.showCancel = false
							}).then(() => {
								this.getOrderList('待接单', 0, true)
							})
						}
					}
				})
			},
			//
			hidePhoneNumber(phoneNumber) {
			    if (!phoneNumber || phoneNumber.length !== 11) {
			      return phoneNumber; // 如果电话号码为空或长度不是11位，直接返回
			    }
			    return phoneNumber.slice(0, 3) + '****' + phoneNumber.slice(7);
			},
			// 查看地图
			goMap(data) {
				uni.openLocation({
					latitude: data.lat,
					longitude: data.lng,
					name: data.address,
					// address: data.citynames,
					success: function() {
						console.log('success');
					}
				});
			},
			// 打电话
			call(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;
		box-shadow: $u-color-border-shadow;
	
		.top {
			display: flex;
			justify-content: space-between;
	
			.left {
				align-items: center;
			}
	
			.right {
				color: $u-type-warning-dark;
			}
		}
	
		.msg-box {
			.u-flex {
				align-items: baseline;
			}
	
			.usertype {
				font-size: 32rpx;
				padding-top: 10rpx;
			}
	
			.address {
				color: $u-tips-color;
				padding-top: 10rpx;
			}
		}
	
		.timefrom {
			color: $u-tips-color;
		}
	
		.item {
			display: flex;
			margin: 20rpx 0 0;
			justify-content: space-between;
	
			.left {
				margin-right: 20rpx;
	
				image {
					width: 160rpx;
					height: 160rpx;
					border-radius: 10rpx;
				}
			}
	
			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}
	
				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}
	
				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}
	
			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;
	
				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}
	
				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}
	
		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;
	
			.total-price {
				font-size: 32rpx;
			}
		}
	
		.time {
			font-size: 24rpx;
			margin: 20rpx 0;
			line-height: 40rpx;
		}
	
		.bottom {
			display: flex;
			margin-top: 20rpx;
			// padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;
	
			.btn {
				line-height: 80rpx;
				width: 160rpx;
				border-radius: 8rpx;
				border: 2rpx solid $u-border-color;
				font-size: 32rpx;
				text-align: center;
				color: $u-type-info-dark;
				margin-left: 10rpx;
				flex: 1;
			}
	
			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
			
			.details{
				background-color: $u-type-primary;
				border-color: $u-type-primary;
				color: #ffffff;
			}
		}
		
		.more{
			display: flex;
			align-items: center;
			line-height: 32rpx;
			.cell{
				flex: 1;
				text-align: center;
			}
			.divider{
				border-left: 1px solid $u-border-color;
				height: 40rpx;
			}
		}
	}
</style>